package org.cuit.lmq.controller;


import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.cuit.lmq.constants.enums.TrainTypeEnum;
import org.cuit.lmq.domain.common.BaseContextHolder;
import org.cuit.lmq.domain.common.Result;
import org.cuit.lmq.domain.po.TrainInfo;
import org.cuit.lmq.exception.CustomException;
import org.cuit.lmq.service.TrainInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Seon
 * @since 2023-03-03
 */
@Slf4j
@RestController
@RequestMapping("/data/train")
public class TrainInfoController {

    @Autowired
    private TrainInfoService trainInfoService;

    @PostMapping("/add")
    public Result<?> addTrainInfo(@RequestBody TrainInfo trainInfo) {
        // 使用枚举类进行参数校验
        TrainTypeEnum.isExist(trainInfo.getTrainType().getType());
        trainInfo.setUserId(BaseContextHolder.getCurrentUserId());
        boolean save = trainInfoService.save(trainInfo);
        if (save) {
            return Result.success(true);
        } else {
            return Result.fail("添加失败，请稍后重试", 0);
        }
    }

    @DeleteMapping("/del")
    public Result<?> addTrainInfo(@RequestBody Map<String, Object> data) {
        Long trainDataId = Long.parseLong((String) data.get("trainDataId"));
        if (ObjectUtils.isEmpty(trainDataId)) {
            return Result.fail("参数错误，请重试", HttpStatus.HTTP_BAD_REQUEST);
        }
        boolean remove = trainInfoService.removeById(trainDataId);
        if (remove) {
            return Result.success(true);
        } else {
            return Result.fail("删除失败，请稍后重试", 0);
        }
    }


    @PostMapping("/page")
    public Result<?> getDataBetweenDate(@RequestBody Map<String, String> data) {
        String date = data.get("date");
        if (StringUtils.isBlank(date)) {
            throw new CustomException("参数格式错误");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse = sdf.parse(date);
            // 一天的毫秒数，用于相加
            long multiply = (1000 * 60 * 60 * 24) - 1000;
            long multiDate = parse.getTime() + multiply;
            Date addDate = new Date(multiDate);
            LambdaQueryWrapper<TrainInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TrainInfo::getUserId, BaseContextHolder.getCurrentUserId());
            wrapper.between(TrainInfo::getDate, date, addDate);
            wrapper.orderByDesc(TrainInfo::getCreateTime);
            List<TrainInfo> list = trainInfoService.list(wrapper);
            return Result.success(list);
        } catch (ParseException e) {
            log.error("{}", e.toString());
            return Result.fail("日期格式错误，请重试", 0);
        }
    }

}

